package com.sgl.config.quarz;


import com.sgl.entity.AcceptPayOrder;
import com.sgl.entity.MybatisParamVo;
import com.sgl.entity.OrderHistory;
import com.sgl.service.AcceptPayOrderService;
import com.sgl.service.DictService;
import com.sgl.service.OrderService;
import com.sgl.threads.DoNotify;
import com.sgl.util.PageBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 定义任务类
 * 此任务 用于 检查订单订单超时
 *  如果订单超过 后后台设定的时间之后 将订单的状态改为 超时未处理
 *  时间由字典表后台可以管理
 */


@Service
@DisallowConcurrentExecution //禁止并发执行
public class CheckOrderTimeOut implements Job, ApplicationRunner {

    @Autowired
    DictService dictService;

    @Autowired
    OrderService orderService;

    private static final Log log = LogFactory.getLog(CheckOrderTimeOut.class);


   private  ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);

   @Autowired
    AcceptPayOrderService acceptPayOrderService;

    /**
     * 任务被触发时所执行的方法
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String hours=dictService.findByKeywords("hours").getValue();
        log.info("===============检查是否有存在超时的订单  时间默认每半小时检查一次====================");
        PageBean<AcceptPayOrder> orderPageBean = new PageBean<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("status","0");
        param.put("hours",hours);
        orderPageBean.setParam(param);
        List<AcceptPayOrder> list = acceptPayOrderService.findList(orderPageBean);
        log.info("===============检查到"+list.size() +"笔订单已失效====================");
        if (list.size()>0){
            MybatisParamVo<AcceptPayOrder> paramVo = new MybatisParamVo<>();
            paramVo.setStatus(2);
            paramVo.setParams(list);
            Integer result = acceptPayOrderService.changeStatusByParam(paramVo);
            if (result>0){
                log.info("===============修改了"+list.size() +"笔订单为已失效====================");
            }
        }else{
            log.info("===============没有失效订单===================");
        }


    }

    public void test() throws JobExecutionException {
        String hours=dictService.findByKeywords("hours").getValue();
        log.info("===============   检查是否有存在超时的订单  ==========");
        PageBean<AcceptPayOrder> orderPageBean = new PageBean<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("status","0");
        param.put("hours",hours);
        orderPageBean.setParam(param);
        List<AcceptPayOrder> list = acceptPayOrderService.findList(orderPageBean);
        log.info("===============检查到"+list.size() +"笔订单已失效====================");
        if (list.size()>0){
            MybatisParamVo<AcceptPayOrder> paramVo = new MybatisParamVo<>();
            paramVo.setStatus(2);
            paramVo.setParams(list);
            Integer result = acceptPayOrderService.changeStatusByParam(paramVo);
            if (result>0){
                log.info("===============修改了"+list.size() +"笔订单为已失效====================");
            }
        }else{
            log.info("===============没有失效订单===================");
        }


    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

    }

}
